package com.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @authoer zsj
 * @date 2023/4/13 20:41
 */
public class Demo {

    //声明窗口对象
    JFrame f = new JFrame("测试窗口组件");

    //声明菜单相关组件
    JMenuBar jMenuBar = new JMenuBar();

    //主菜单
    JMenu fileMenu = new JMenu("文件");
    JMenu editMenu = new JMenu("编辑");

    //菜单零件
    JMenuItem auto = new JMenuItem("自动换行");
    JMenuItem copy = new JMenuItem("复制");
    JMenuItem paste = new JMenuItem("粘贴");


    //主菜单
    JMenu format =  new JMenu("格式");
    //菜单零件
    JMenuItem commented = new JMenuItem("注释");
    JMenuItem cancel_commented = new JMenuItem("取消注释");


    //文本域
    JTextArea jTextArea = new JTextArea(8,20);


    String[] colors = {"红色","绿色","蓝色"};
    //声明列表框
    JList<String> colorList = new JList<>(colors);

    //声明选择相关组件
    JComboBox<String> colorSelect = new JComboBox<>();

    //声明单选框
    JRadioButton male = new JRadioButton("男",true);
    JRadioButton female = new JRadioButton("女",false);

    //复选框
    JCheckBox marry = new JCheckBox("是否已婚？",true);


    //声明底部
    JTextField tf = new JTextField(40);
    JButton ok = new JButton("确定");


    //声明右键菜单
    JPopupMenu jPopupMenu = new JPopupMenu();

    //按钮组
    ButtonGroup buttonGroup = new ButtonGroup();
    JRadioButtonMenuItem metal = new JRadioButtonMenuItem("metal风格");
    JRadioButtonMenuItem nimbus = new JRadioButtonMenuItem("nimbus风格");
    JRadioButtonMenuItem windows = new JRadioButtonMenuItem("windows风格",true);
    JRadioButtonMenuItem windows_classic = new JRadioButtonMenuItem("windows_classic风格");
    JRadioButtonMenuItem motif = new JRadioButtonMenuItem("motif风格");











    //方法初始化
    public void init(){
        //组装视图
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(tf);
        bottomPanel.add(ok);
        f.add(bottomPanel, BorderLayout.SOUTH);

        //组装选择相关的组件
        JPanel selectPanel = new JPanel();

        colorSelect.addItem("红色");
        colorSelect.addItem("绿色");
        colorSelect.addItem("蓝色");

        selectPanel.add(colorSelect);
        buttonGroup.add(male);
        buttonGroup.add(female);
        selectPanel.add(male);
        selectPanel.add(female);
        selectPanel.add(marry);

        //组装文本域和选择相关组件
        Box topLeft = Box.createVerticalBox();
        topLeft.add(jTextArea);
        topLeft.add(selectPanel);

        //组装顶部
        Box top = Box.createHorizontalBox();
        top.add(topLeft,colorList);

        f.add(top);

        //菜单里的格式的两个选项
        format.add(commented);
        format.add(cancel_commented);


        editMenu.add(auto);
        editMenu.addSeparator();
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.addSeparator();
        editMenu.add(format);


        jMenuBar.add(fileMenu);
        jMenuBar.add(editMenu);

        f.setJMenuBar(jMenuBar);

        //组装右键菜单
        buttonGroup.add(metal);
        buttonGroup.add(windows);
        buttonGroup.add(nimbus);
        buttonGroup.add(windows_classic);
        buttonGroup.add(motif);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                //todo  写一个函数修改主题
            }
        };
        metal.addActionListener(actionListener);
        windows.addActionListener(actionListener);
        windows_classic.addActionListener(actionListener);
        nimbus.addActionListener(actionListener);
        motif.addActionListener(actionListener);

        jPopupMenu.add(metal);
        jPopupMenu.add(windows);
        jPopupMenu.add(windows_classic);
        jPopupMenu.add(motif);
        jPopupMenu.add(nimbus);

        tf.setComponentPopupMenu(jPopupMenu);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);



    }


    public static void main(String[] args) {
        new Demo().init();
    }




}
